/**
 * 加载GLTF模型
 */
let LoadGltf = (function () {

    function LoadGltf(viewer) {
        this.viewer = viewer;
        this.yuanquEntities = [];
        this.oldColor = undefined;
        this.oldColorBlendAmount = undefined;
        this.oldColorBlendMode = undefined;
        this.hander = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
        this.debounce = this.createDebounce();
    }

    LoadGltf.prototype.loadGltf = function () {
        for (let i = 0; i <= 10; i++) {
            let index = i;
            if (index == 0) index = -1;
            let position = Cesium.Cartesian3.fromDegrees(117.1725, 31.8195, 150);
            let hpr = new Cesium.HeadingPitchRoll(Cesium.Math.toRadians(25), Cesium.Math.toRadians(0), Cesium.Math.toRadians(0));
            let orientation = Cesium.Transforms.headingPitchRollQuaternion(
                position,
                hpr
            );
            let entity = {
                id: 'yuanqu_model_' + i,
                name: '园区模型',
                position: position,
                orientation: orientation,
                model: {
                    uri: baseUrl + '/gltf/floor/' + index + '.glb',
                    scale: 3.0,
                },
            };
            this.viewer.entities.add(entity);
            this.yuanquEntities.push(entity);
        }

        this.hander.setInputAction(e => {
            this.debounce(() => {
                let pickedFeature = this.viewer.scene.pick(e.endPosition);
                if (!pickedFeature || !pickedFeature.id || !pickedFeature.id.id) {
                    this.resetColor();
                    return;
                }
                if (pickedFeature.id.id.indexOf('yuanqu_model_') == -1) return;

                this.resetColor();

                let pickedEntity = this.viewer.entities.getById(pickedFeature.id.id);

                if (!this.oldColor) {
                    this.oldColor = pickedEntity.model.color;
                    this.oldColorBlendAmount = pickedEntity.model.colorBlendAmount;
                    this.oldColorBlendMode = pickedEntity.model.colorBlendMode;
                }

                pickedEntity.model.color = Cesium.Color.fromCssColorString("#ffff00");
                pickedEntity.model.colorBlendAmount = 0.8;
                pickedEntity.model.colorBlendMode = 1;
            });
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

        this.hander.setInputAction(e => {
            let pickedFeature = this.viewer.scene.pick(e.position);
            if (!pickedFeature || !pickedFeature.id || !pickedFeature.id.id) return;
            if (pickedFeature.id.id.indexOf('yuanqu_model_') == -1) return;

            console.info('单击了模型id=' + pickedFeature.id.id);
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }

    LoadGltf.prototype.resetColor = function () {
        this.yuanquEntities.map(item => {
            let entity = this.viewer.entities.getById(item.id);
            entity.model.color = this.oldColor;
            entity.model.colorBlendAmount = this.oldColorBlendAmount;
            entity.model.colorBlendMode = this.oldColorBlendMode;
        });
    }

    /**
     * 函数防抖
     */
    LoadGltf.prototype.createDebounce = function (waitTime) {
        let timeout;

        return (func) => {
            if (timeout) {
                clearTimeout(timeout);
            }

            timeout = setTimeout(() => {
                timeout = undefined;
                func();
            }, waitTime || 100);
        };
    }

    return LoadGltf;
})();
